package com.yzkj.siot.exp;

import com.yzkj.siot.result.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
@Component
@Slf4j
public class GlobalExpHandler {

    @ExceptionHandler(BizExp.class)
    public ResponseResult<Object> trySiotBizException(HttpServletRequest req, HandlerMethod handlerMethod, BizExp e) {

        log.info("业务异常", e);
        return new ResponseResult<>(e.getErrorCode(), Boolean.FALSE, e.getErrorMsg(), null);
    }

    @ExceptionHandler(SysExp.class)
    public ResponseResult<Object> trySiotSysException(HttpServletRequest req, HandlerMethod handlerMethod, BizExp e) {

        log.error("系统异常", e);
        return new ResponseResult<>(e.getErrorCode(), Boolean.FALSE, e.getErrorMsg(), null);
    }

    @ExceptionHandler(Exception.class)
    public ResponseResult<Object> tryRuntimeException(HttpServletRequest req, HandlerMethod handlerMethod, RuntimeException e) {
        log.error("系统运行未知异常", e);
        return new ResponseResult<>(-1, Boolean.FALSE, "服务器未知错误", null);
    }
}
